<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="baidu-site-verification" content="code-UjJKh3jtOo">
  <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.5.0/styles/atom-one-dark.min.css">
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  <title>Tit1e</title>
  <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2767111597631391" crossorigin="anonymous"></script>
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Tit1e" type="application/atom+xml">
</head>

<body class="dark">
    <div class="container">
    <header class="header">
  <div class="title">
      <a href="/" class="logo">Tit1e</a>
      <div class="btn-dark"></div>
      <script>
        let bodyClx = document.body.classList;
        let btnDark = document.querySelector('.btn-dark');
        let sysDark = window.matchMedia('(prefers-color-scheme: dark)');
        let darkVal = localStorage.getItem('dark');

        let setDark = (isDark) => {
        bodyClx[isDark ? 'add' : 'remove']('dark');
        localStorage.setItem('dark', isDark ? 'yes' : 'no');
        };

        setDark(darkVal ? darkVal === 'yes' : sysDark.matches);
        requestAnimationFrame(() => bodyClx.remove('not-ready'));

        btnDark.addEventListener('click', () => setDark(!bodyClx.contains('dark')));
        sysDark.addEventListener('change', (event) => setDark(event.matches));
      </script>
  </div>
    <ul class="menu">
        
        
        
        
        <li class="menu-item ">
            <a href="/" class="menu-item-link">Home</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/archives/" class="menu-item-link">Archives</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/atom.xml" class="menu-item-link">RSS</a>
        </li>
        
        
        <li class="menu-item ">
            <a target="_blank" rel="noopener" href="https://pixeltimer.art" class="menu-item-link">Pixel Timer</a>
        </li>
        
    </ul>
    <div class="search-box">
        <input type="search" id="search" placeholder="搜索">
    </div>
</header>
    <article class="post">
  <div class="post-meta">
    <span class="post-time">12 / 21, 2017</span>
  </div>
  <div class="post-title">
    <h1 class="title">局域网请求 webpack 反向代理</h1>
  </div>
  
  <ul class="post-tags">
    
    <li class="tag-item">
      #学习
    </li>
    
    <li class="tag-item">
      #vue
    </li>
    
  </ul>
  
  <div class="post-content">
    <p>今天继续在公司思考关于前后端分离的问题，困扰我最大的问题其实就是我用 vue-cli 在本地开发的时候，怎么和后端进行接口的调试，总不可能每次都打包一下放到服务器上看运行正不正常，有没有报错吧？我知道有个反向代理的东西，可是知道它的大致作用，但是我没用过导致我无从下手，但前段时间折腾模拟数据的时候，有用到用<code>json-server</code>和<code>faker.js</code>本地开服务器，批量生成模拟数据，然后通过反向代理去请求本地另一个服务器的数据。于是我想，既然这样我可以请求到数据，那么局域网的话我也可以直接通过 ip 直接访问局域网内的服务器，那么我应该也可以通过代理去请求局域网上内其他同事开的本地服务器中的接口，于是就开始折腾了。</p>
<span id="more"></span>
<p>由于之前去请求模拟数据的时候，代理配置是写在<code>/config/index.js</code>中的，于是我依样画葫芦，把之前的配置的地址，路径全改了，但结果却发现请求一只 404 ，这就让我很难受了，于是我去 Google 了一圈，原来这个代理是用的<code>webpack-dev-server</code>，于是我找到了<code>/build/webpack.dev.conf.js</code>,按照网上说的，在<code>devServer</code>中添加了一个<code>proxy</code>属性</p>
<pre><code class="javascript">proxy: &#123;
  &#39;/Home&#39;: &#123;
    target: &#39;http://192.168.1.1/&#39;,
    secure: false
  &#125;
&#125;
</code></pre>
<p>这样，我就成功通过代理请求到了同事服务器上的数据。</p>
<p>突然想起来了，利用<code>json-server</code>和<code>faker.js</code>模拟数据的过程还没记录，下篇文章记录一下。</p>

  </div>
  <div class="post-near">
    
    <a
      class="post-near-prev"
      href="/p/6c8e565e.html"
      title="vuex的简单使用教程"
    >
      <span class="arrow">←</span>
      <span class="post-near-title">
        vuex的简单使用教程
      </span>
    </a>
    
    
    <a
      class="post-near-next"
      href="/p/6b4d2c5e.html"
      title="element-theme 配合 vue-cli 进行二次开发"
    >
      <span class="post-near-title">
        element-theme 配合 vue-cli 进行二次开发
      </span>
      <span class="arrow">→</span>
    </a>
    
  </div>
</article>

    <footer class="footer">
  <p>本博客采用 <a class="license-link" rel="license noopener" target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际</a>协议进行许可</p>
  <p>
    <span>Thanks For <a target="_blank" rel="noopener" href="https://github.com/nanxiaobei/hugo-paper" taget="_blank">hugo-paper</a></span>
    <span style="margin-left: 10px;">&copy; 2016-2025 by Tit1e</span>
  </p>
</footer>
    </div>
    <script src="//unpkg.com/@highlightjs/cdn-assets@11.5.0/highlight.min.js"></script>
<script>
  hljs.initHighlightingOnLoad();
  var _hmt = _hmt || [];
    (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?6349d3bc54baea0b04a65145c5e3b799";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
    })();
    const block = document.querySelectorAll('.post-content')[0]
    block.addEventListener('click', e => {
        const {nodeName, src} = e.target
        if(nodeName !== 'IMG') return
        document.body.classList.add('overflow-hidden')
        const div = document.createElement('div')
        div.id = 'global-cover'
        div.title = '点击关闭'
        const a = document.createElement('a')
        a.href = src
        a.target="_blank"
        a.title = '点击打开原图'
        const img = document.createElement('img')
        img.className = 'show-pic'
        img.src = src
        a.appendChild(img)
        div.appendChild(a)
        document.body.appendChild(div)
    })
    document.body.addEventListener('click', e => {
        if(e.target.id === 'global-cover'){
            document.body.classList.remove('overflow-hidden')
            document.body.removeChild(e.target)
        }
    })
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7M04VN3JZ4"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-7M04VN3JZ4');
</script>

<script src="/js/search.js"></script>


</body>
</html>